package com.ezcoding.dao;

import com.ezcoding.model.Message;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

public class MessageDao {
    public static MessageDao dao = new MessageDao();

    public List<Message> getMessages(String username) {
        List<Message> messages = getReceiveMessage(username);

        messages.addAll(getSendMessages(username));

        return messages;
    }

    private List<Message> getSendMessages(String username) {
        String sql = Db.getSql("Message.getSendMessages");

        return Message.dao.find(sql, username);
    }

    private List<Message> getReceiveMessage(String username) {
        String sql = Db.getSql("Message.getReceiveMessage");

        return Message.dao.find(sql, username);
    }

    public Message getMessage(String username, String dingdanId) {
        String sql = Db.getSql("Message.getMessageBySourceUsernameAndDingdanId");

        return Message.dao.findFirst(sql, username, dingdanId);
    }

    public Message getMessageById(String username, String messageId) {
        String sql = Db.getSql("Message.getMessageByUsernameAndMessageId");

        return Message.dao.findFirst(sql, username, messageId);
    }

    public List<Message> getImportMessages(String username, int type) {
        String sql = Db.getSql("Message.getImportMessages");

        return Message.dao.find(sql, username, type);
    }
}
